﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using AnguaRPGEngine.Library.Systems;

namespace AnguaRPGEngine.Library.Dice
{
    public class D100 : IDie
    {
        #region ctor
        public D100()
        {
        }
        #endregion
        #region IDie Members
        #region Image
        public System.Drawing.Bitmap Image
        {
            get
            {
                Font frontFirstDigitFont = new Font(FontFamily.GenericSansSerif, 20f);
                Font frontSecondDigitFont = new Font(FontFamily.GenericSansSerif, 16f);
                Font sideFont = new Font(FontFamily.GenericSansSerif, 12f);
                Bitmap i = ImageLibrary.Instance.GetImageFromLibrary(string.Format("Dice.{0}.D{1}", DieRoller.Instance.DiceCollection.CurrentSet.PathName, Sides, _Value));
                using (Graphics g = Graphics.FromImage(i))
                {
                    string tensSpot = "00";
                    if (Value > 9 && Value < 100)
                    {
                        int tens = Value - (Value % 10);
                        tensSpot = string.Format("{0}", tens);
                    }
                    string onesSpot = string.Format("{0}", Value % 10);

                    if (onesSpot == "6" || onesSpot == "9")
                    {
                        g.DrawLine(new Pen(new SolidBrush(DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor()), 1f), new Point(92, 52), new Point(102, 52));
                    }
                    if (SelectSideToTheLeft(onesSpot) == "6" || SelectSideToTheLeft(onesSpot) == "9")
                    {
                        g.DrawLine(new Pen(new SolidBrush(Color.FromArgb(80, DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor())), 1f), new Point(65, 33), new Point(70, 41));
                    }
                    if (SelectSideToTheRight(onesSpot) == "6" || SelectSideToTheRight(onesSpot) == "9")
                    {
                        g.DrawLine(new Pen(new SolidBrush(Color.FromArgb(100, DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor())), 1f), new Point(123, 44), new Point(128, 42));
                    }


                    g.DrawString(tensSpot.Substring(0, 1), frontFirstDigitFont, new SolidBrush(DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor()), new PointF(9, 18));
                    g.DrawString(tensSpot.Substring(1, 1), frontSecondDigitFont, new SolidBrush(DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor()), new PointF(24, 22));

                    Matrix matrix = new Matrix();
                    matrix.Rotate(35);
                    g.Transform = matrix;
                    g.DrawString(SelectSideAbove(tensSpot), sideFont, new SolidBrush(Color.FromArgb(100, DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor())), new PointF(25, -20));
                    g.ResetTransform();
                    matrix.Rotate(-65);
                    g.Transform = matrix;
                    g.DrawString(SelectSideBelow(tensSpot), sideFont, new SolidBrush(Color.FromArgb(100, DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor())), new PointF(-10, 60));

                    g.ResetTransform();
                    g.DrawString(onesSpot, frontFirstDigitFont, new SolidBrush(DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor()), new PointF(86, 23));

                    matrix.Rotate(80);
                    g.Transform = matrix;
                    g.DrawString(SelectSideToTheLeft(onesSpot), frontFirstDigitFont, new SolidBrush(Color.FromArgb(80, DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor())), new PointF(60, -54));
                    g.ResetTransform();
                    matrix.Rotate(-80);
                    g.Transform = matrix;
                    g.DrawString(SelectSideToTheRight(onesSpot), frontFirstDigitFont, new SolidBrush(Color.FromArgb(100, DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor())), new PointF(75, 73));
                }
                return i;
            }
        }
        #endregion
        #region Sides
        public int Sides
        {
            get
            {
                return 100;
            }
        }
        #endregion
        #region Value
        private int _Value = 1;
        public int Value
        {
            get
            {
                return _Value;
            }
        }
        #endregion
        #region Roll
        public void Roll()
        {
            _Value = Randomizer.Instance.NextInt(1, Sides);
        }
        #endregion
        #endregion
        private string SelectSideAbove(string tensSpot)
        {
            int ts = int.Parse(tensSpot);
            switch (ts)
            {
                case 10:
                    return "90";
                case 20:
                    return "60";
                case 30:
                    return "70";
                case 40:
                    return "00";
                case 50:
                    return "30";
                case 60:
                    return "40";
                case 70:
                    return "10";
                case 80:
                    return "20";
                case 90:
                    return "50";
            }
            return "80";
        }
        private string SelectSideBelow(string tensSpot)
        {
            int ts = int.Parse(tensSpot);
            switch (ts)
            {
                case 10:
                    return "70";
                case 20:
                    return "80";
                case 30:
                    return "50";
                case 40:
                    return "60";
                case 50:
                    return "90";
                case 60:
                    return "20";
                case 70:
                    return "30";
                case 80:
                    return "00";
                case 90:
                    return "10";
            }
            return "40";
        }
        private string SelectSideToTheLeft(string onesSpot)
        {
            int ts = int.Parse(onesSpot);
            switch (ts)
            {
                case 1:
                    return "9";
                case 2:
                    return "6";
                case 3:
                    return "7";
                case 4:
                    return "0";
                case 5:
                    return "3";
                case 6:
                    return "4";
                case 7:
                    return "1";
                case 8:
                    return "2";
                case 9:
                    return "5";
            }
            return "8";
        }
        private string SelectSideToTheRight(string onesSpot)
        {
            int ts = int.Parse(onesSpot);
            switch (ts)
            {
                case 1:
                    return "7";
                case 2:
                    return "8";
                case 3:
                    return "5";
                case 4:
                    return "6";
                case 5:
                    return "9";
                case 6:
                    return "2";
                case 7:
                    return "3";
                case 8:
                    return "0";
                case 9:
                    return "1";
            }
            return "4";
        }
    }
}
